#! /bin/sh
#exec 2>>build.log  ##编译过程打印到日志文件中
## 服务重启批量执行 .Power by terrfly

## 第一个参数必须是项目名称或者all
## example : ./restartJavaServer.sh agent start  || ./restartJavaServer.sh all restart

#引入config
. ./config.sh

declare -a cmdList=("start" "stop" "restart")


##↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓定义函数↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓

##
function hasCmd() ## $2=需要执行的命令
{

	for v in ${!cmdList[@]};do
		if test "$1" = "${cmdList[$v]}"; then
		  return 1
		fi
	done

	return 0
}

function callProject() ##$1=项目名称  $2执行命令参数
{
	local appName="$1"
    if [ ! -n "$appName" ];then
        echo "ERROR: appName is null"
         exit 0
    fi

	echo "" ##换行
   
	##必须 进入程序bin目录 ，否是日志将在当前目录打印   = =。
	cd $javaProjectInstallPath/$appName
	./app.sh $2
	
	sleep 10
	
	cd $currentPath
}

##↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑定义函数↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑

##如第一个参数为null 提示错误信息.
if [ ! -n "$1" ];then
	echo "ERROR: please input appName [${javaProjectMap[*]}]"
	exit 0
fi

##如第二个参数为null 提示错误信息.
if [ ! -n "$2" ];then
	echo "ERROR: please input cmd [${cmdList[*]}]"
	exit 0
fi

checkJavaProjectName $1
if [ $? != 1 ];then
	echo "ERROR: appName[$1] is not available!"
	exit 0
fi

hasCmd $2 ##调用hasVal,判断项目名是否合法，并返回结果。 1-通过
##当第一个参数不等于all并且不在projects数组中
if [ $? != 1 ];then
	echo "ERROR: cmd[$2] is not available!"
	exit 0
fi

if [ "$1" == "all"  ];then	
	for v in ${!javaProjectMap[@]};do
		callProject "${javaProjectMap[$v]}" $2
	done
else
	callProject $1 $2
fi

echo ""
echo '【Done.】'
